<template>
  <div class="app-container">
    <el-card style="margin-bottom: 10px">
      <el-row :gutter="24" style="margin-bottom: -15px">
        <el-col :span="24" :xs="24">
          <el-form
            :model="queryParams"
            ref="queryForm"
            :inline="true"
            v-show="showSearch"
            label-width="108px"
            @submit.native.prevent
          >
            <el-form-item label="发票所属公司" prop="name">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item style="float: right">
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>

    <el-card style="margin-top: 10px">
      <stylizedTable
        title="发票信息"
        :buttonList="buttonList"
        :selector="false"
        :ordinal="false"
        :queryParams="queryParams"
        :loading="false"
        :total="total"
        :tableItem="tableItem"
        :tableData="InvoiceInformationList"
        @reset="getList"
        @getList="getList"
      />
    </el-card>

    <!-- 添加或修改发票信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="45%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="公司名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司税号" prop="taxNumber">
              <el-input v-model="form.taxNumber" placeholder="请输入公司税号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="开户银行" prop="bank">
              <el-input v-model="form.bank" placeholder="请输入开户银行" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="bankAccount">
              <el-input
                v-model="form.bankAccount"
                placeholder="请输入银行账号"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="联系电话" prop="telephone">
              <el-input
                v-model="form.telephone"
                placeholder="请输入联系电话"
                v-number-input
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInvoiceInformation,
  getInvoiceInformation,
  delInvoiceInformation,
  addInvoiceInformation,
  updateInvoiceInformation,
} from "@/api/contract/InvoiceInformation";

export default {
  name: "InvoiceInformation",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票信息表格数据
      InvoiceInformationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        taxNumber: null,
        bank: null,
        bankAccount: null,
        telephone: null,
        address: null,
        state: null,
      },
      buttonList: [
        {
          icon: "el-icon-circle-plus-outline",
          type: "primary",
          text: "添加",
          methods: this.handleAdd,
        },
      ],
      tableItem: [
        {
          porp: "公司名称",
          data: "name",
          solt: { name: 1 },
        },
        {
          porp: "公司税号",
          data: "taxNumber",
          solt: { name: 1 },
        },
        {
          porp: "开户银行",
          data: "bank",
          solt: { name: 1 },
        },
        {
          porp: "银行账号",
          data: "bankAccount",
          solt: { name: 1 },
        },
        {
          porp: "联系电话",
          data: "telephone",
          solt: { name: 1 },
        },
        {
          porp: "地址",
          data: "address",
          solt: { name: 1 },
        },
        {
          porp: "操作",
          fixed: "right",
          width: 200,
          solt: {
            name: 5,
            operate: [
              {
                text: "复制",
                icon: "copy",
                methods: this.copyToClipboard,
                mark: "复制",
                markIcon: "copy02",
              },
              {
                text: "修改",
                icon: "editTable",
                methods: this.handleUpdate,
                mark: "修改",
                markIcon: "editTable02",
              },
              {
                text: "停用",
                icon: "stop",
                methods: this.handleStatusChange,
                mark: "启用",
                markIcon: "stop02",
                methods02: this.handleStatusChange,
              },
            ],
          },
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "公司名称不能为空", trigger: "blur" },
        ],
        taxNumber: [
          { required: true, message: "公司税号不能为空", trigger: "blur" },
        ],
        bank: [
          { required: true, message: "开户银行不能为空", trigger: "blur" },
        ],
        bankAccount: [
          { required: true, message: "银行账号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发票信息列表 */
    getList() {
      this.loading = true;
      listInvoiceInformation(this.queryParams).then((response) => {
        this.InvoiceInformationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //复制方法
    async copyToClipboard(str) {
      console.log("1111");
      const text = `
公司名称:${str.name}
公司税号:${str.taxNumber}
开户银行:${str.bank}
银行账号:${str.bankAccount}
     `.trim(); // 去除多余空白
      try {
        await navigator.clipboard.writeText(text);
        this.$modal.msgSuccess("复制成功");
        return;
      } catch (error) {
        console.error(error);
      }
      const el = document.createElement("textarea");
      el.value = text;
      el.setAttribute("readonly", "");
      el.style.position = "absolute";
      el.style.left = "-9999px";
      document.body.appendChild(el);
      const selected =
        document.getSelection().rangeCount > 0
          ? document.getSelection().getRangeAt(0)
          : false;
      el.select();
      document.execCommand("copy");
      document.body.removeChild(el);
      if (selected) {
        document.getSelection().removeAllRanges();
        document.getSelection().addRange(selected);
      }
      this.$modal.msgSuccess("复制成功");
    },
    //启用/禁用方法
    handleStatusChange(row) {
      console.log(row);
      // 保存原始状态
      let originalStatus = row.state;
      let actionText = row.state === "0" ? "停用" : "启用";

      this.$confirm(`确认要${actionText}'${row.name}'吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning",
      })
        .then(() => {
          // 根据状态设置新的值
          let newStatus = row.state === "1" ? "0" : "1";
          console.log(row.invoiceInformationId);
          console.log(newStatus);
          let body = {
            invoiceInformationId: row.invoiceInformationId,
            state: newStatus,
          };
          updateInvoiceInformation(body).then(() => {
            this.$modal.msgSuccess("操作成功");
            this.getList(); // 成功后刷新列表
          });
        })
        .catch(() => {
          // 取消时恢复原来的状态
          row.status = originalStatus;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        invoiceInformationId: null,
        name: null,
        taxNumber: null,
        bank: null,
        bankAccount: null,
        telephone: null,
        address: null,
        state: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.invoiceInformationId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加发票信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const invoiceInformationId = row.invoiceInformationId || this.ids;
      getInvoiceInformation(invoiceInformationId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发票信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.invoiceInformationId != null) {
            updateInvoiceInformation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoiceInformation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const invoiceInformationIds = row.invoiceInformationId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除发票信息编号为"' + invoiceInformationIds + '"的数据项？'
        )
        .then(function () {
          return delInvoiceInformation(invoiceInformationIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "contract/InvoiceInformation/export",
        {
          ...this.queryParams,
        },
        `InvoiceInformation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
::v-deep .el-dialog {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  margin: 0px !important;
}
::v-deep .el-dialog__header {
  background-color: rgb(248, 250, 251); /* 你可以根据需求修改颜色 */
  color: rgb(99, 86, 110); /* 标题文字颜色 */
}
</style>
